﻿using Abp.BackgroundJobs;
using Abp.Dependency;
using Abp.Runtime.Caching;
using FwProject.Comm;
using FwProject.PublicService.JPush;
using System;
using System.Collections.Generic;
using System.Text;
using Abp.Domain;
using Abp.Domain.Repositories;
using FwProject.Authorization.Users;
using Abp.Domain.Uow;
using Castle.Core.Logging;
using FwProject.JPush.JPushModel;

namespace FwProject.BackGround.AppShopPay
{
    public class PayNotifyBackground : BackgroundJob<PaySuccessData>, ITransientDependency
    {
        public readonly ICacheManager _catchmanager;
        public readonly IJPushAppservice _jPushAppservice;
        public readonly IRepository<User,long> _repositoryUser;
        public ILogger logger { get; set; }
        public  PayNotifyBackground(ICacheManager catchmanager,
                IJPushAppservice jPushAppservice,
                IRepository<User, long> repositoryUser
            )
        {
            _repositoryUser = repositoryUser;
            _catchmanager = catchmanager;
            _jPushAppservice = jPushAppservice;
            logger = NullLogger.Instance;
        }
        [UnitOfWork]
        public override void Execute(PaySuccessData args)
        {
            try
            {
                using (CurrentUnitOfWork.DisableFilter(AbpDataFilters.MayHaveTenant))
                {
                 
                    var model = _repositoryUser.Get(args.UserId);
                    PayNotifyPushMessagModel pushModel = new PayNotifyPushMessagModel();
                    pushModel.TopicModel = JPush.JPushHelpel.TopicRestauran;
                    pushModel.UserId = args.UserId;
                    pushModel.OrderNumber = args.PayData.OrderNumber;
                    pushModel.Id = args.PayData.Id;
                    _jPushAppservice.RestaurantPushMessage(model.Mobile, pushModel, $"【饭碗易购消息提示：订单支付成功,支付方式{args.PayType}（订单编号：【{args.PayData.OrderNumber}】交易金额：{args.PayData.PayTotal}）】我们将尽快安排发货你注意即时查收");
                    CurrentUnitOfWork.SaveChanges();
                }
            }
            catch (Exception exe)
            {

                logger.Error("订单支付成功通知失败", exe);
            }
           
        }
    }
}
